package com.cskaoyan.shopping.converter;


import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.dto.UpdateCartRequest;
import com.cskaoyan.shopping.form.CartForm;
import org.springframework.stereotype.Component;

@Component
public class CartItemConverter {


    public static CartProductDto item2Dto(Item item) {
        CartProductDto cartProduct = new CartProductDto();
        cartProduct.setProductId(item.getId());
        cartProduct.setProductName(item.getTitle());
        cartProduct.setSalePrice(item.getPrice());
        cartProduct.setProductImg(item.getImages()[0]);
        if (item.getLimitNum() == null) {
            cartProduct.setLimitNum(Long.valueOf(item.getNum()));
        } else if (item.getLimitNum() < 0 || item.getNum() < 0) {
            cartProduct.setLimitNum((long) 0);
        } else {
            cartProduct.setLimitNum(Long.valueOf(item.getLimitNum()));
        }
        return cartProduct;
    }

    public static UpdateCartRequest cartFormToUpdateCartRequest(CartForm cartForm) {
        UpdateCartRequest updateCartRequest = new UpdateCartRequest();
        updateCartRequest.setUserId(cartForm.getUserId());
        updateCartRequest.setItemId(cartForm.getProductId());
        updateCartRequest.setNum(cartForm.getProductNum());
        updateCartRequest.setChecked(cartForm.getChecked());
        return updateCartRequest;
    }
}
